package top.zuli.package02.homework14;

public class AccountTest {
    public static void main(String[] args) {
        Account account = new Account(0);
        Thread t1 = new Customer(account);
        Thread t2 = new Customer(account);
        t1.start();
        t2.start();
    }
}
class Account{
    private int money;

    public Account(int money) {
        this.money = money;
    }

    public synchronized void saveMoney(int money) {
        if(money>0){
            this.money+=money;
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"：存钱成功。余额为："+this.money);
        }
    }
}
class Customer extends Thread{
    private Account account;
    public Customer(Account account) {
        this.account = account;
    }

    @Override
    public void run() {
        for (int i = 1; i <=3; i++) {
            account.saveMoney(1000);
        }
    }
}